#include<bits/stdc++.h>
using namespace std;
long long T,n,m,x;
int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    for(long long i=1;i<=n+m;i++)
    {
        cin>>x;
    }
    cin>>T;
    while(T--)
    {
        cin>>x>>x>>x>>x;
        cout<<pow(2,ceil(log2(n)));
    }
    return 0;
}
